<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>2_回调函数形式的ref</title>
</head>

<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>
    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <script type="text/javascript" src="../js/prop-types.js"></script>
    <script type="text/babel">
        // 创建组件
        class Demo extends React.Component {
            // 展示左侧输入框的数据
            showData = () => {
                const { value } = this.input1;
                alert(value);
            };
            // 展示右侧输入框的数据
            showData2 = () => {
                const { value } = this.input2;
                alert(value);
            };
            // 渲染组件结构
            render() {
                return (
                    <div>
                        <input
                            ref={(c) => (this.input1 = c)}
                            type="text"
                            placeholder="点击按钮提示数据"
                        />
                        &nbsp;
                        <button onClick={this.showData}>点我提示左侧数据</button>&nbsp;
                        <input
                            ref={(c) => (this.input2 = c)}
                            onBlur={this.showData2}
                            type="text"
                            placeholder="失去焦点提示数据"
                        />
                    </div>
                );
            }
        }
        // 渲染组件到页面
        ReactDOM.render(<Demo />, document.getElementById("test"));
    </script>
</body>

</html>